import SwiftUI
import UIKit

struct LoginView: View {
    @EnvironmentObject private var appEnvironment: AppEnvironment
    @State private var phone: String = ""
    @State private var password: String = ""
    @State private var isPasswordVisible: Bool = false
    @State private var agreed: Bool = false
    @State private var showAgreementAlert: Bool = false

    var body: some View {
        VStack(spacing: AppSpacing.l) {
            // 顶部标题与描述
            VStack(alignment: .leading, spacing: 8) {
                Text("Login")
                    .font(.system(size: 32, weight: .bold))
                Text("Login now to track all your expenses")
                    .font(.system(size: 12))
                    .foregroundStyle(.secondary)
                Text("and income at a place!")
                    .font(.system(size: 12))
                    .foregroundStyle(.secondary)
            }
            .frame(maxWidth: .infinity, alignment: .leading)

            // 输入区域
            VStack(alignment: .leading, spacing: 12) {
                Text("手机号").font(.system(size: 12)).foregroundStyle(.primary)
                    .padding(.bottom, 2)
                HStack {
                    Image(systemName: "iphone").foregroundStyle(.gray).frame(width: 20)
                    TextField("请输入手机号", text: $phone)
                        .keyboardType(.numberPad)
                        .textContentType(.telephoneNumber)
                }
                .frame(height: 25)
                .padding()
                .background(Color.gray.opacity(0.1))
                .cornerRadius(8)

                Text("密码").font(.system(size: 12)).foregroundStyle(.primary)
                    .padding(.bottom, 2)
                HStack {
                    Image(systemName: "lock.fill").foregroundStyle(.gray).frame(width: 20)
                    if isPasswordVisible {
                        TextField("请输入密码", text: $password)
                    } else {
                        SecureField("请输入密码", text: $password)
                    }
                    Button(action: { isPasswordVisible.toggle() }) {
                        Image(systemName: isPasswordVisible ? "eye.slash" : "eye")
                            .foregroundStyle(.secondary)
                    }
                }
                .frame(height: 25)
                .padding()
                .background(Color.gray.opacity(0.1))
                .cornerRadius(8)

                HStack {
                    NavigationLink("Forgot Password?", destination: ForgotPasswordStep1View())
                        .font(.system(size: 12, weight: .bold))
                        .foregroundStyle(Color.accentColor)
                        .underline(true, color: .accentColor)
                    Spacer()
                    NavigationLink("Register", destination: RegisterStep1View())
                        .font(.system(size: 12, weight: .bold))
                        .foregroundStyle(Color.accentColor)
                        .underline(true, color: .accentColor)
                }
            }

            // 登录按钮
            Button(action: {
                if !agreed { showAgreementAlert = true }
            }) {
                Text("Login")
                    .foregroundStyle(.white)
                    .frame(maxWidth: .infinity)
            }
            .buttonStyle(.primary)

            // 协议勾选
            HStack(spacing: 6) {
                Button(action: { agreed.toggle() }) {
                    Image(systemName: agreed ? "checkmark.circle.fill" : "circle")
                }
                Text("I've read and agreed to the User Agreement and Privacy Policy")
                    .font(.system(size: 12))
            }

            Spacer()
        }
        .padding(.horizontal, AppSpacing.xl)
        .padding(.top, UIScreen.main.bounds.height * 0.1)
        .navigationTitle("")
        .navigationBarTitleDisplayMode(.inline)
        .background(Color(.systemBackground).ignoresSafeArea())
        .contentShape(Rectangle())
        .onTapGesture { dismissKeyboard() }
        .overlay {
            if showAgreementAlert {
                CustomAlertView(
                    isPresented: $showAgreementAlert,
                    isSingleButton: false,
                    icon: "exclamationmark.triangle.fill",
                    iconColor: .yellow,
                    title: "阅读提醒",
                    message: "请先阅读并同意用户协议与隐私政策，以继续登录。",
                    closeButtonText: "取消",
                    confirmButtonText: "同意",
                    onConfirm: { agreed = true }
                )
            }
        }
    }
}

#Preview {
    NavigationStack { LoginView() }
        .environmentObject(AppEnvironment())
}

private extension LoginView {
    func dismissKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}


